﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Tools
{
    public static class SessionKey
    {
        public static void SetSessionKey(string userID,string userName)
        {
            Tools.SessionDictionary.SetSession(HttpContext.Current.Session, "AsynchronousUserID", userID);
            Tools.SessionDictionary.SetSession(HttpContext.Current.Session, "AsynchronousLoginTime", DateTime.Now);
            Tools.SessionDictionary.SetSession(HttpContext.Current.Session, "Username", userName);
            Tools.SessionDictionary.SetSession(HttpContext.Current.Session, "Lock", false);
        }

        public static string CheckKey()
        {
            Dictionary<string, object> S = Tools.SessionDictionary.GetSession(HttpContext.Current.Session.SessionID);
            if (S.ContainsKey("Lock"))
                if ((bool)S["Lock"])
                    return null;
            if (HttpContext.Current.Session["AsynchronousUserID"] != null)
                return HttpContext.Current.Session["AsynchronousUserID"].ToString();
            else
                return null;
        }

        public static string CheckKeyThrow()
        {
            Dictionary<string, object> S = Tools.SessionDictionary.GetSession(HttpContext.Current.Session.SessionID);
            if (S.ContainsKey("Lock"))
                if ((bool)S["Lock"])
                    throw new Exception("当前帐号被管理临时锁定，请与管理员联系。");
            if (HttpContext.Current.Session["AsynchronousUserID"] != null)
                return HttpContext.Current.Session["AsynchronousUserID"].ToString();
            else
                throw new Exception("没有检测到验证信息，请遵从正常的流程进行操作或者重新登陆再试。");
        }

        public static string CheckKey(string SessionID)
        {
            Dictionary<string, object> S = Tools.SessionDictionary.GetSession(HttpContext.Current.Session.SessionID);
            if (S.ContainsKey("Lock"))
                if ((bool)S["Lock"])
                    return null;
            if (HttpContext.Current.Session["AsynchronousUserID"] != null)
                return HttpContext.Current.Session["AsynchronousUserID"].ToString();
            else if (SessionDictionary.GetSession(SessionID).ContainsKey("AsynchronousUserID"))
            {
                HttpContext.Current.Session["AsynchronousUserID"] = SessionDictionary.GetSession(SessionID)["AsynchronousUserID"];
                return SessionDictionary.GetSession(SessionID)["AsynchronousUserID"].ToString();
            }
            else
                return null;
        }

        public static string CheckKeyThrow(string SessionID)
        {
            Dictionary<string, object> S = Tools.SessionDictionary.GetSession(HttpContext.Current.Session.SessionID);
            if (S.ContainsKey("Lock"))
                if ((bool)S["Lock"])
                    throw new Exception("当前帐号被管理临时锁定，请与管理员联系。");
            if (HttpContext.Current.Session["AsynchronousUserID"] != null)
                return HttpContext.Current.Session["AsynchronousUserID"].ToString();
            else if (SessionDictionary.GetSession(SessionID).ContainsKey("AsynchronousUserID"))
            {
                HttpContext.Current.Session["AsynchronousUserID"] = SessionDictionary.GetSession(SessionID)["AsynchronousUserID"];
                return SessionDictionary.GetSession(SessionID)["AsynchronousUserID"].ToString();
            }
            else
                throw new Exception("没有检测到验证信息，请遵从正常的流程进行操作或者重新登陆再试。");
        }

        public static string GetUsername()
        {
            return HttpContext.Current.Session["Username"].ToString();
        }

        public static string GetUserID()
        {
            return HttpContext.Current.Session["AsynchronousUserID"].ToString();
        }
    }
}
